<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/4
 * Time: 14:53
 */

namespace app\api\model;

use think\Db;
class TaobaoorderModel extends BaseModel
{
    public function orderlsit(){//淘宝订单列表
        $sort = request()->post('sort');
        $data = Db::name('taobao')
            ->where('status',1)
          	->where('sort',$sort)
            ->select();
      return $data;
    }
  	public function search(){
      	$search = request()->post('search');
          	$res = Db::name('taobao')
                ->where('status',1)
                ->where('goods','like','%'.$search.'%')
                ->order('id desc')
                ->select();
      	if(empty($res)){
        	$res = Db::name('taobao')
                ->where('status',1)
                ->order('id desc')
                ->select();
        }
      return $res;
    } 
       
  	public function type(){//查询店铺类型
     	return
            $res=Db::name('taobao_type')
                ->select();
    }
    public function ordercheck(){//淘宝详情查看
        $id = request()->post('id');
        return Db::name('taobao')
            ->where('id',$id)
            ->find();
    }
    public function Confirmorder($uid){
        $commodity_id = request()->post('commodity_id');
      	$sel=Db::name('goods')
          ->where('id',$commodity_id)
          ->find();
        $number = request()->post('number');
        $price = $number*$sel['price'];
        $aid = request()->post('aid');
        $createtime=time();
        $order_number=$createtime.rand(1000,9999);
        if($commodity_id==''||$price==''||$aid==''||$number==''){//数据不能为null
            return '2';
        }
        $rs=Db::name('balance')//查用户余额
        ->where('uid',$uid)
            ->find();
        /*$re=Db::name('goods')//查店铺库存
        ->where('id',$commodity_id)
            ->field('stock')
            ->find();*/
        $circulate=$rs['circulate']-$price;
        if ($circulate<0){//用户余额不足
            return '3';
        }
        /*$numbers=$re['stock']-$number;//商店库存
        if($numbers<0){//如果店铺库存小于购买数量报错
            return '4';
        }*/
      	
        $res=Db::name('taobao_order')//添加订单
        ->insert([
            'commodity_id' =>$commodity_id,
            'price' =>$price,
            'aid' =>$aid,
            'number'=>$number,
            'status'=>1,
            'createtime'=>$createtime,
            'order_number'=>$order_number,
            'uid'=>$uid
        ]);
        /*$up=Db::name('goods')
            ->where('id',$commodity_id)
            ->update(['stock'=>$numbers]);//修改店铺的库存*/
        return $ups=Db::name('balance')//查用户余额
        ->where('uid',$uid)
            ->update(['circulate'=>$circulate]);//修改账户余额

    }
  	public function address($uid){//查默认地址		
      return
     	 $res=Db::name('area')
           ->join('xst_user','xst_user.id=xst_area.uid')
           ->join('xst_balance','xst_balance.uid=xst_user.id')
            ->where('xst_area.uid',$uid)
        	->where('xst_area.flg',1)
           ->field('xst_area.*,xst_balance.circulate')
            ->find();
    }
  	public function order_list($uid){//订单列表
       $status = request()->post('status');
       if($status==0){
    	return $res=Db::name('taobao_order')
          	->join('xst_taobao tb','tb.id=xst_taobao_order.commodity_id')
        	->join('xst_taobao_type t','tb.sort=t.id')
            ->where('uid',$uid)
          	->field('xst_taobao_order.*,tb.goods,tb.price univalent,tb.small_img,t.name')
            ->select();
          }else{
        //echo 1;die;
          return $res=Db::name('taobao_order')
                ->join('xst_taobao tb','tb.id=xst_taobao_order.commodity_id')
                ->join('xst_taobao_type t','tb.sort=t.id')
            	->where('uid',$uid)
                ->where('xst_taobao_order.status',$status)
                ->field('xst_taobao_order.*,tb.goods,tb.price univalent,tb.small_img,t.name')
                ->select();
      }
    }
    public function order_check($uid){//订单列表
       //$status = request()->post('status');
       $id = request()->post('id');
      
      	return $res=Db::name('taobao_order')
        	->join('xst_taobao tb','bt.id=xst_taobao_order.commodity_id')
        	->join('xst_taobao_type t','tb.sort=t.id')
            ->where('xst_taobao_order.id',$id)
        	->field('xst_taobao_order.*,tb.goods,tb.price univalent,tb.small_img,t.name')
            ->find();
    }
    public function complete($uid){//完成订单
        $id = request()->post('id');
        return $res=Db::name('taobao_order')
            ->where('id',$id)
            ->update(['status'=>3]);
    }
}